package com.urea.web.controller.urea;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.io.FileUtil;
import com.urea.common.utils.QrCodeUtils;
import com.urea.system.domain.vo.TerminalVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.urea.common.annotation.Log;
import com.urea.common.core.controller.BaseController;
import com.urea.common.core.domain.AjaxResult;
import com.urea.common.enums.BusinessType;
import com.urea.system.domain.Terminal;
import com.urea.system.service.ITerminalService;
import com.urea.common.utils.poi.ExcelUtil;
import com.urea.common.core.page.TableDataInfo;

/**
 * 终端Controller
 * 
 * @author liuzd
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/terminal")
@Api(tags = "终端信息")
public class TerminalController extends BaseController
{
    @Autowired
    private ITerminalService terminalService;

    /**
     * 查询终端列表
     */
//    @PreAuthorize("@ss.hasPermi('terminal:terminal:list')")
    @GetMapping("/list")
    @ApiOperation("終端列表")
    public TableDataInfo list(TerminalVo vo)
    {
        startPage();
        List<TerminalVo> list = terminalService.selectTerminalList(vo);
        return getDataTable(list);
    }

    /**
     * 导出终端列表
     */
    @PreAuthorize("@ss.hasPermi('terminal:terminal:export')")
    @Log(title = "终端", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TerminalVo terminal)
    {
        List<TerminalVo> list = terminalService.selectTerminalList(terminal);
        ExcelUtil<TerminalVo> util = new ExcelUtil<TerminalVo>(TerminalVo.class);
        util.exportExcel(response, list, "终端数据");
    }

    /**
     * 获取终端详细信息
     */
//    @PreAuthorize("@ss.hasPermi('terminal:terminal:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("根据终端id获取终端信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(terminalService.selectTerminalById(id));
    }

    /**
     * 获取终端详细信息
     */
    @GetMapping(value = "getInfoByNo")
    @ApiOperation("根据设备号获取设备信息")
    public AjaxResult getInfoByNo(@RequestParam("terminalNo") String terminalNo)
    {
        return success(terminalService.getInfoByNo(terminalNo));
    }

    /**
     * 新增终端
     */
//    @PreAuthorize("@ss.hasPermi('terminal:terminal:add')")
    @Log(title = "终端", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增终端")

    public AjaxResult add(@RequestBody TerminalVo terminal)
    {
        terminalService.insertTerminal(terminal);
        return success();
    }

    /**
     * 修改终端
     */
//    @PreAuthorize("@ss.hasPermi('terminal:terminal:edit')")
    @Log(title = "终端", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    @ApiOperation("修改终端信息")
    public AjaxResult edit(@RequestBody TerminalVo terminal)
    {
        terminalService.updateTerminal(terminal);
        return success();
    }
    @Log(title = "终端分配", businessType = BusinessType.UPDATE)
    @PostMapping("assign")
    @ApiOperation("终端分配")
    public AjaxResult assign(@RequestBody TerminalVo terminal)
    {
        terminalService.assignTerminal(terminal);
        return success();
    }

    /**
     * 删除终端
     */
//    @PreAuthorize("@ss.hasPermi('terminal:terminal:remove')")
    @Log(title = "终端", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(terminalService.deleteTerminalByIds(ids));
    }


    /**
     * 获取附近的列表
     * @param longitude 经度
     * @param latitude 纬度
     * @return 设备列表
     */
    @GetMapping("/vicinityList")
    @ApiOperation("附近列表")
    public AjaxResult vicinityList(@ApiParam(name = "经度",value = "longitude")@RequestParam("longitude") String longitude,
                                   @ApiParam(name = "纬度",value = "latitude")@RequestParam("latitude")String latitude){
        return AjaxResult.success(terminalService.vicinityList(longitude,latitude));

    }


    @GetMapping("/genTerminalCode")
    @ApiOperation("生成设备二维码")
    public void genTerminalCode(@ApiParam(name = "连接",value = "url")@RequestParam("url") String url,HttpServletResponse response){
        BufferedImage bufferedImage = QrCodeUtils.buildQrCodeImage(url);
        try {
            ImageIO.write(bufferedImage,"jpg",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取代理商或门店列表
     * @param type 01代理商 04门店
     * @return 列表
     */
    @GetMapping("/queryUserList")
    @ApiOperation("获取代理商或渠道商列表")
    public AjaxResult queryUserList(@ApiParam(name = "type",value = "01代理商 04门店")@RequestParam("type")String type){
        return AjaxResult.success(terminalService.queryUserList(type));
    }


    @GetMapping("/statistics")
    @ApiOperation("pc代理商统计")
    public AjaxResult statistics(){
        return success(terminalService.statistics());
    }
}
